<?php
namespace Admin\Controller;

use Think\Verify;
use Common\Base\ControllerBase;
use Common\Logic\Admin\AuthLogic;

class AuthController extends ControllerBase
{
    public $msg = array(
        1=>'未知错误',
        2=>'登出成功',
        3=>'验证码错误'
    );

    public function login()
    {
        if(is_admin_login() > 0)
        {
            redirect(U('index/index'));
        }
        if(IS_POST)
        {
            $post = I('post.');

            //验证码
            $verify = new Verify();
            $ck = $verify->check($post['verify'], 1);

            if($ck)
            {
                $L_auth = new AuthLogic();
                $rs = $L_auth->login($post);
                if($rs['st'] == 'n')
                {
                    $this->assign('error_msg', $rs['note']);
                }
                elseif($rs['st'] == 'y')
                {
                    redirect(U('index/index'), 0);
                }
                else
                {
                $this->assign('error_msg', $this->msg[1]);
                }
            }else{
                $this->assign('error_msg', $this->msg[3]);
            }
        }
        $this->display();
    }

    //退出登录
    public function logout()
    {
        if(is_admin_login() > 0)
        {
            session(null);
            $this->redirect(U('auth/login'));
        }
        else
        {
            $this->redirect('login');
        }
    }

    //验证码
    public function verify()
    {
        $verify = new Verify(C('verify_cfg'));
        $verify->entry(1);
    }

    public function _empty()
    {
        $this->redirect(U('auth/login'));
    }
}